package com.xii.msmservice.service.Impl;

import com.aliyun.dysmsapi20170525.models.SendSmsRequest;
import com.aliyun.dysmsapi20170525.models.SendSmsResponse;
import com.aliyun.teaopenapi.models.Config;
import com.xii.msmservice.service.MsmService;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Map;

@Service
public class MsmServiceImpl implements MsmService {

    /**
     * 注册没通过暂不可用  使用测试阿里云
     * @param phone
     * @param param
     * @return
     */
//    @Override
//    public boolean send(String phone, Map<String, Object> param) {
//        if(StringUtils.isEmpty(phone)) return false;
//        DefaultProfile profile =
//                DefaultProfile.getProfile("default", "LTAI5tAfVL8z2XS7kifne2d9", "D7ZnweBh6KkLwrzUYDOx63JsIlQvGs");
//        IAcsClient client = new DefaultAcsClient(profile);
//
//        //设置固定参数
//        CommonRequest request = new CommonRequest();
//        //request.setProtocol(ProtocolType.HTTPS);
//        request.setMethod(MethodType.POST);
//        request.setDomain("dysmsapi.aliyuncs.com");
//        request.setVersion("2017-05-25");
//        request.setAction("SendSms");
//        request.putQueryParameter("PhoneNumbers", phone);   //手机号
//        request.putQueryParameter("SignName", "阿里云短信测试");
//        request.putQueryParameter("TemplateCode", "SMS_154950909");   //阿里云测试API
////        request.putQueryParameter("TemplateParam", JSONObject.toJSONString(param));
//        try {
//            CommonResponse response = client.getCommonResponse(request);
//            System.out.println(response.getData());
//            return response.getHttpResponse().isSuccess();
//        } catch (ServerException e) {
//            e.printStackTrace();
//        } catch (ClientException e) {
//            e.printStackTrace();
//        }
//        return false;
//    }


    /**
     * @description: 阿里云官网测试短信发送 无需认证
     *
     * @return {@link null}
     * @author  wangxihao
     * @email wangxh0108@163.com
    **/
    public static com.aliyun.dysmsapi20170525.Client createClient(String accessKeyId, String accessKeySecret) throws Exception {
        Config config = new Config()
                // 您的AccessKey ID
                .setAccessKeyId(accessKeyId)
                // 您的AccessKey Secret
                .setAccessKeySecret(accessKeySecret);
        // 访问的域名
        config.endpoint = "dysmsapi.aliyuncs.com";
        return new com.aliyun.dysmsapi20170525.Client(config);
    }

    /**
     * demo
     * @param args_
     * @throws Exception
     */
    public static void main(String[] args_) throws Exception {
        java.util.List<String> args = java.util.Arrays.asList(args_);
        com.aliyun.dysmsapi20170525.Client client = MsmServiceImpl.createClient("LTAI5tAfVL8z2XS7kifne2d9", "D7ZnweBh6KkLwrzUYDOx63JsIlQvGs");
        SendSmsRequest sendSmsRequest = new SendSmsRequest()
                .setSignName("阿里云短信测试")
                .setTemplateCode("SMS_154950909")
                .setPhoneNumbers("18303663712")
                .setTemplateParam("{\"code\":\"1234\"}");
        // 复制代码运行请自行打印 API 的返回值
        client.sendSms(sendSmsRequest);
    }




    /**
     * @description: 使用测试demo
     *
     * @return {@link }
     * @author  wangxihao
     * @email wangxh0108@163.com
    **/
    @Override
    public boolean send(String phone, Map<String, Object> param) {
        if(StringUtils.isEmpty(phone)) return false;
        try {
            //设置固定参数
            com.aliyun.dysmsapi20170525.Client client = MsmServiceImpl.createClient("LTAI5tAfVL8z2XS7kifne2d9", "D7ZnweBh6KkLwrzUYDOx63JsIlQvGs");
            SendSmsRequest sendSmsRequest = new SendSmsRequest()
                    .setSignName("阿里云短信测试")
                    .setTemplateCode("SMS_154950909")
                    .setPhoneNumbers(phone)
                    .setTemplateParam("{\"code\":\""+(String) param.get("code")+"\"}");
            // 复制代码运行请自行打印 API 的返回值
            SendSmsResponse sendSmsResponse = client.sendSms(sendSmsRequest);
            //打印参数
            System.out.println(sendSmsResponse.getBody().toString());
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        }

        return false;
    }

}
